package com.sduept.nwld.dataserver.controller.base.primary;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.model.core.PowerSystemResource;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.wires.*;
import com.sduept.nwld.dataserver.manager.cim.BxCimManager;
import com.sduept.nwld.dataserver.model.edata.cim.bx.*;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Named
@ViewScoped
public class BxDataModelController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7287741437493546930L;
	@Autowired
	private BxCimManager bxCimManger;

	private List<BxSubstation> bxSubstations = new ArrayList<>();
	private List<BxACLineSegment> bxACLineSegments = new ArrayList<>();
	private List<BxBusbarsection> bxBusbarsections = new ArrayList<>();
	private List<BxBreaker> bxBreakers = new ArrayList<>();
	private List<BxPowertransformer> bxPowertransformers = new ArrayList<>();
	private List<BxCompensator> bxCompensators = new ArrayList<>();
	private BxSubstation selectedBxSub;
	private BxACLineSegment selectedBxAcline;
	private BxBusbarsection selectedBusbarSection;
	private BxBreaker selectedBxBreaker;
	private BxPowertransformer selectedPower;
	private BxCompensator selectedBxCompensator;
	private BxHydrogeneratingunit selectedBxGenerating ;
	private List<BxHydrogeneratingunit> bxGeneratings;
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	private void init() {
	
		bxSubstations = bxCimManger.getBxSubstation(true);
		bxACLineSegments = (List<BxACLineSegment>) bxCimManger
				.getBxPrimaryEqupiment(true, BxACLineSegment.class);
		bxBusbarsections = (List<BxBusbarsection>) bxCimManger
				.getBxPrimaryEqupiment(true, BxBusbarsection.class);
		bxBreakers = (List<BxBreaker>) bxCimManger.getBxPrimaryEqupiment(true,
				BxBreaker.class);
		bxPowertransformers = (List<BxPowertransformer>) bxCimManger
				.getBxPrimaryEqupiment(true, BxPowertransformer.class);
		bxCompensators = (List<BxCompensator>) bxCimManger
				.getBxPrimaryEqupiment(true,BxCompensator.class);
		bxGeneratings = (List<BxHydrogeneratingunit>) bxCimManger
				.getBxPrimaryEqupiment(true,BxHydrogeneratingunit.class);

	}
	public void chooseEquipment(String type) {
        Map<String,Object> options = new HashMap<String, Object>();
        options.put("resizable", true);
        options.put("draggable", true);
        options.put("modal", false);
        options.put("width", 600);
        options.put("height", 400);
        Map<String,List<String>> params = new HashMap<>();
        List<String> pl = new ArrayList<>();
        pl.add(type);
        params.put("selectedType", pl);
        openDialog("/ui/common/equipment_selector_single",params, options );
    }
	public void onSelectedStationChosen(SelectEvent event) {
		//返回的是List<Substation>
		if(event.getObject() != null){
			Substation s = (Substation) event.getObject();
				selectedBxSub.setCimId(s.getId());
				selectedBxSub.setCimName(s.getName());
				bxCimManger.saveBxSubstation(selectedBxSub);
		}
	}
	public void onAclineChosen(SelectEvent event) {
		//返回的是List<Substation>
		if(event.getObject() != null){
			ACLineSegment s = (ACLineSegment) event.getObject();
			selectedBxAcline.setCimId(s.getId());
			selectedBxAcline.setCimName(s.getName());
			bxCimManger.saveBxACLineSegment(selectedBxAcline);
		}
	}
	public void onBusbarSectionChosen(SelectEvent event) {
		//返回的是List<Substation>
		if(event.getObject() != null){
			BusbarSection s = (BusbarSection) event.getObject();
			selectedBusbarSection.setCimId(s.getId());
			selectedBusbarSection.setCimName(s.getName());
			bxCimManger.saveBxBusbarsection(selectedBusbarSection);
		}
	}
	public void onBreakerChosen(SelectEvent event) {
		//返回的是List<Substation>
		if(event.getObject() != null){
			Breaker s = (Breaker) event.getObject();
			selectedBxBreaker.setCimId(s.getId());
			selectedBxBreaker.setCimName(s.getName());
			bxCimManger.saveBxBreaker(selectedBxBreaker);
		}
	}
	public void onPowerChosen(SelectEvent event) {
		//返回的是List<Substation>
		if(event.getObject() != null){
			PowerTransformer s = (PowerTransformer) event.getObject();
			selectedPower.setCimId(s.getId());
			selectedPower.setCimName(s.getName());
			bxCimManger.saveBxPowertransformer(selectedPower);
		}
	}
	public void onCompensatorChosen(SelectEvent event) {
		//返回的是List<Substation>
		if(event.getObject() != null){
			Compensator s = (Compensator) event.getObject();
			selectedBxCompensator.setCimId(s.getId());
			selectedBxCompensator.setCimName(s.getName());
			bxCimManger.saveBxCompensator(selectedBxCompensator);
		}
	}
	public void onGeneratingChosen(SelectEvent event) {
		//返回的是List<Substation>
		if(event.getObject() != null){
			PowerSystemResource s = (PowerSystemResource) event.getObject();
			selectedBxGenerating.setCimId(s.getId());
			selectedBxGenerating.setCimName(s.getName());
			bxCimManger.saveBxHydrogeneratingunit(selectedBxGenerating);
		}
	}
	/**
	 * 关联变电站
	 */
	public void relationSubstation() {
		//log.info(bxCimManger.relationSubstation());
		addSuccessMessage("关联完毕");
		bxSubstations = bxCimManger.getBxSubstation(true);
	}
	/**
	 * 获取所有变电站
	 */
	public void getAllBxSubstation() {
		bxSubstations = bxCimManger.getBxSubstation(false);
	}
	/**
	 * 获取未关联变电站
	 */
	public void getBxSubstation() {
		bxSubstations = bxCimManger.getBxSubstation(true);
	}
	/**
	 * 关联 线路
	 */
	public void relation500kVAcline() {
		//log.info(bxCimManger.relation500kVAcline());
		addSuccessMessage("关联完毕");
	}
	/**
	 * 获取所有线路
	 */
	@SuppressWarnings("unchecked")
	public void getAllAcline() {
		bxACLineSegments = (List<BxACLineSegment>) bxCimManger
				.getBxPrimaryEqupiment(false, BxACLineSegment.class);
	}
	/**
	 * 获取 未关联线路
	 */
	@SuppressWarnings("unchecked")
	public void getAcline() {
		bxACLineSegments = (List<BxACLineSegment>) bxCimManger
				.getBxPrimaryEqupiment(true, BxACLineSegment.class);
	}

	public void relation500kVBusbar() {
		bxCimManger.relation500kVBusbar();
		addSuccessMessage("关联完毕");
	}

	@SuppressWarnings("unchecked")
	public void getAllBusbars() {
		bxBusbarsections = (List<BxBusbarsection>) bxCimManger
				.getBxPrimaryEqupiment(false, BxBusbarsection.class);
	}

	@SuppressWarnings("unchecked")
	public void getBusbar() {
		bxBusbarsections = (List<BxBusbarsection>) bxCimManger
				.getBxPrimaryEqupiment(true, BxBusbarsection.class);
	}

	public void relation500kVBreaker() {
		bxCimManger.relation500kVBreaker();
		addSuccessMessage("关联完毕");
	}

	@SuppressWarnings("unchecked")
	public void getAllBreaker() {
		bxBreakers = (List<BxBreaker>) bxCimManger.getBxPrimaryEqupiment(false,
				BxBreaker.class);
		// bxBreakers.clear();
	}

	@SuppressWarnings("unchecked")
	public void getBreaker() {
		bxBreakers = (List<BxBreaker>) bxCimManger.getBxPrimaryEqupiment(true,
				BxBreaker.class);
	}

	public void relation500kVPowerTrans() {
		bxCimManger.relation500kVPowerTrans();
		addSuccessMessage("关联完毕");
	}

	@SuppressWarnings("unchecked")
	public void getAllPowerTrans() {
		bxPowertransformers = (List<BxPowertransformer>) bxCimManger
				.getBxPrimaryEqupiment(false, BxPowertransformer.class);
	}

	@SuppressWarnings("unchecked")
	public void getPowerTrans() {
		bxPowertransformers = (List<BxPowertransformer>) bxCimManger
				.getBxPrimaryEqupiment(true, BxPowertransformer.class);
	}
	@SuppressWarnings("unchecked")
	public void getAllBxCompensator(){
		bxCompensators = (List<BxCompensator>) bxCimManger
				.getBxPrimaryEqupiment(false,BxCompensator.class);
	}
	@SuppressWarnings("unchecked")
	public void getBxCompensator(){
		bxCompensators = (List<BxCompensator>) bxCimManger
				.getBxPrimaryEqupiment(true,BxCompensator.class);
	}
	public List<BxSubstation> getBxSubstations() {
		return bxSubstations;
	}
	/**
	 * 关联发电机组
	 */
	@SuppressWarnings("unchecked")
	public void relationGenerating() {
		//log.info(bxCimManger.relationGenerating());
		addSuccessMessage("关联完毕");
		bxGeneratings = (List<BxHydrogeneratingunit>) bxCimManger
				.getBxPrimaryEqupiment(true,BxHydrogeneratingunit.class);
	}
	@SuppressWarnings("unchecked")
	public void getAllGeneratings(){
		bxGeneratings = (List<BxHydrogeneratingunit>) bxCimManger
				.getBxPrimaryEqupiment(false,BxHydrogeneratingunit.class);
	}
	@SuppressWarnings("unchecked")
	public void getGeneratings(){
		bxGeneratings = (List<BxHydrogeneratingunit>) bxCimManger
				.getBxPrimaryEqupiment(true,BxHydrogeneratingunit.class);
	}
	public void setBxSubstations(List<BxSubstation> bxSubstations) {
		this.bxSubstations = bxSubstations;
	}

	public List<BxACLineSegment> getBxACLineSegments() {
		return bxACLineSegments;
	}

	public void setBxACLineSegments(List<BxACLineSegment> bxACLineSegments) {
		this.bxACLineSegments = bxACLineSegments;
	}

	public List<BxBusbarsection> getBxBusbarsections() {
		return bxBusbarsections;
	}

	public void setBxBusbarsections(List<BxBusbarsection> bxBusbarsections) {
		this.bxBusbarsections = bxBusbarsections;
	}

	public List<BxBreaker> getBxBreakers() {
		return bxBreakers;
	}

	public void setBxBreakers(List<BxBreaker> bxBreakers) {
		this.bxBreakers = bxBreakers;
	}

	public List<BxPowertransformer> getBxPowertransformers() {
		return bxPowertransformers;
	}

	public void setBxPowertransformers(
			List<BxPowertransformer> bxPowertransformers) {
		this.bxPowertransformers = bxPowertransformers;
	}


	public BxSubstation getSelectedBxSub() {
		return selectedBxSub;
	}

	public void setSelectedBxSub(BxSubstation selectedBxSub) {
		this.selectedBxSub = selectedBxSub;
	}

	public BxACLineSegment getSelectedBxAcline() {
		return selectedBxAcline;
	}

	public void setSelectedBxAcline(BxACLineSegment selectedBxAcline) {
		this.selectedBxAcline = selectedBxAcline;
	}
	public BxBusbarsection getSelectedBusbarSection() {
		return selectedBusbarSection;
	}
	public void setSelectedBusbarSection(BxBusbarsection selectedBusbarSection) {
		this.selectedBusbarSection = selectedBusbarSection;
	}
	public BxBreaker getSelectedBxBreaker() {
		return selectedBxBreaker;
	}
	public void setSelectedBxBreaker(BxBreaker selectedBxBreaker) {
		this.selectedBxBreaker = selectedBxBreaker;
	}
	public BxPowertransformer getSelectedPower() {
		return selectedPower;
	}
	public void setSelectedPower(BxPowertransformer selectedPower) {
		this.selectedPower = selectedPower;
	}
	public List<BxCompensator> getBxCompensators() {
		return bxCompensators;
	}
	public void setBxCompensators(List<BxCompensator> bxCompensators) {
		this.bxCompensators = bxCompensators;
	}
	public BxCompensator getSelectedBxCompensator() {
		return selectedBxCompensator;
	}
	public void setSelectedBxCompensator(BxCompensator selectedBxCompensator) {
		this.selectedBxCompensator = selectedBxCompensator;
	}
	public BxHydrogeneratingunit getSelectedBxGenerating() {
		return selectedBxGenerating;
	}
	public void setSelectedBxGenerating(BxHydrogeneratingunit selectedBxGenerating) {
		this.selectedBxGenerating = selectedBxGenerating;
	}
	public List<BxHydrogeneratingunit> getBxGeneratings() {
		return bxGeneratings;
	}
	public void setBxGeneratings(List<BxHydrogeneratingunit> bxGeneratings) {
		this.bxGeneratings = bxGeneratings;
	}
}
